/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.tedu.mall.product.webapi.config;
import cn.tedu.mall.common.pojo.sso.authentication.CsmallAuthenticationInfo;
import cn.tedu.mall.common.utils.JwtTokenUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@Slf4j
public class WebMvcConfiguration implements WebMvcConfigurer {
    @Autowired
    private JwtTokenUtils jwtTokenUtils;
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        log.debug("product系统开启跨域");
        registry.addMapping("/**")
            .allowedOriginPatterns("*")
            .allowedMethods("*")
            .allowedHeaders("Bearer")
            .allowCredentials(true)
            .maxAge(3600);
    }

   /* @Override public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HandlerInterceptor() {
            //进controller前 拦截鉴权
            @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                Object handler) throws Exception {
                //返回true释放请求
                //获取头 token
                String authorization = request.getHeader("Authorization");
                if (ObjectUtils.isEmpty(authorization)){
                    //自定义 返回response 你没登录,不让进 TODO
                    return false;
                }
                String token=authorization.substring(7);
                if (ObjectUtils.isEmpty(token)){
                    //自定义 返回response 你没登录,不让进 TODO
                    return false;
                }
                CsmallAuthenticationInfo info =null;
                try{
                    info = jwtTokenUtils.getUserInfo(token);
                }catch (Exception e){
                    //自定义 返回response 你没登录,不让进 TODO
                    return false;
                }
                //基本鉴权工作结束,存放权限数据
                request.setAttribute("authorities",info.getAuthorities());
                return true;
                //返回false请求拦截住,自行编写response值
            }
            //controller返回后执行 修改controller返回值
            @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                ModelAndView modelAndView) throws Exception {
                HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
            }
            //响应返回之前,请求结束之前执行 ,释放拦截器中使用的一些资源
            @Override
            public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
                Exception ex) throws Exception {
                HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
            }
        }).addPathPatterns("/**").excludePathPatterns("/login/**");
    }*/
}
